package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import mockup.InviteMockup;
import mockup.MessageMockup;
import mockup.Mockup;

import server.database.ConnectionDatabase;

public class MessageDB implements DatabaseAction {

	private String query;
	private Statement st;

	public MessageDB() {
		try {
			st = ConnectionDatabase.getConnection().createStatement();			
		} catch (Exception e) {
			System.err.println("AppoitmentDB::Constructor:Exception: " + e.getMessage());
		}
	}

	@Override
	public MessageMockup get(int id) {
		if(id> -1) {
			query = "select * from" + ConnectionDatabase.getNameDB() + ".message" +
			" where message_id = " + id;

			ResultSet rs = ConnectionDatabase.get(query, st);
			try {
				while (rs.next()) {
					int message_id = rs.getInt("message_id");
					long sendTime = rs.getLong("sendTime");
					String subject = rs.getString("subject");
					String description = rs.getString("description");
					long readTime = rs.getLong("readtime");
					int is_invite = rs.getInt("is_invite");
					int answer = rs.getInt("answer");
					int meeting_id = rs.getInt("meeting_id");
					int receiver_id = rs.getInt("receiver_int");

					if(is_invite > 0 ) {
						InviteMockup invite = new InviteMockup(receiver_id, meeting_id);
						invite.setId(message_id);
						invite.setSendTime(sendTime);
						invite.setDescription(description);
						invite.setReadTime(readTime);
						invite.setSubject(subject);
						invite.setAnswer(answer);
						return invite;
					} else {
						MessageMockup message = new MessageMockup(message_id, meeting_id, receiver_id, subject, description, sendTime, readTime);
						return message;
					}
				}
			} catch (SQLException e ) {
				System.err.println("MessageDb::get:SQLException: " + e.getMessage());
			}
		} else {
			System.err.println("0 > id , søker ikke i database");
		}
		return null;
	}

	public int insert(Mockup m) {
		int is_invite = 0;
		int answer = 0;
		if(m instanceof InviteMockup) {
			InviteMockup invite = (InviteMockup) m;
			is_invite = 1;
			answer = invite.getAnswer();
		}

		MessageMockup message = (MessageMockup) m;
		query = "INSERT into " + ConnectionDatabase.getNameDB()+".message (" +
		"sendTime, subject, description, readTime, is_invite, answer, meeting_id, receiver_id) " +
		"values(" + message.getSendTime() + ",'" +
		message.getSubject() + "','" + message.getDescription() + "'," + 
		message.getReadTime() + "," + is_invite + "," + answer + "," + message.getMeeting() + ", " +
		message.getReceiver() + ")";

		return ConnectionDatabase.insert(query);
	}

	@Override
	public void update(Mockup m) {
		MessageMockup msg = (MessageMockup) m;

		if(msg.getId() > -1) {
			query = "UPDATE " + ConnectionDatabase.getNameDB() + ".message SET " +
			"sendTime = " + msg.getSendTime() + "," +
			"subject = '" + msg.getSubject() + "'," + 
			"description = '" + msg.getDescription() + "'," + 
			"readTime = " + msg.getReadTime() + "," +
			"meeting_id = " + msg.getMeeting() + "," +
			"receiver_id = " + msg.getReceiver() + ",";

			if(m instanceof InviteMockup) {
				InviteMockup invite = (InviteMockup)m;
				query += "is_invite = 1 " + "," +
				"answer = " + invite.getAnswer() + " ";
			} else {
				query += "is_invite = 0 " + "," +
				"answer = 0 ";
			}

			query += " where message_id = " + msg.getId();
			ConnectionDatabase.update(query, st);

		} else {
			System.err.println("Melding har ikke ID; bruk insert i stedet");
		}
	}

	@Override
	public void delete(Mockup m) {
		MessageMockup message = (MessageMockup) m;
		if(message.getId() > -1) {
			query = "DELETE FROM " + ConnectionDatabase.getNameDB() + ".message " +
			"where message_id = " + message.getId();

			ConnectionDatabase.update(query, st);
		}
	}


	@Override
	public List<MessageMockup> getAll() {
		List<MessageMockup> messages = new ArrayList<MessageMockup>();
		query = "select * from " + ConnectionDatabase.getNameDB() + ".message";
		ResultSet rs = ConnectionDatabase.get(query, st);
		try {
			while (rs.next()) {
				int message_id = rs.getInt("message_id");
				long sendTime = rs.getLong("sendTime");
				String subject = rs.getString("subject");
				String description = rs.getString("description");
				long readTime = rs.getLong("readtime");
				int is_invite = rs.getInt("is_invite");
				int answer = rs.getInt("answer");
				int meeting_id = rs.getInt("meeting_id");
				int receiver_id = rs.getInt("receiver_id");

				if(is_invite > 0 ) {
					InviteMockup invite = new InviteMockup(receiver_id, meeting_id);
					invite.setId(message_id);
					invite.setSendTime(sendTime);
					invite.setDescription(description);
					invite.setReadTime(readTime);
					invite.setSubject(subject);
					invite.setAnswer(answer);
					messages.add(invite);
				} else {
					MessageMockup message = new MessageMockup(message_id, meeting_id, receiver_id, subject, description, sendTime, readTime);
					messages.add(message);
				}
			}
		} catch (SQLException e ) {
			System.err.println("MessageDB::getAll:SQLException: " + e.getMessage());
		}
		return messages;
	}

	@Override
	public void emptyTable() {
		query = "TRUNCATE TABLE " + ConnectionDatabase.getNameDB() + ".message";
		ConnectionDatabase.update(query, st);
	}
}
